package com.mmlc.businessutils;

import android.app.Dialog;
import android.view.View;
import android.widget.Button;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.responsejavabean.RequestError;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.been.OrderButtonBean;
import com.mmlc.network.filter.DeleteOrderRequestFilter;
import com.mmlc.network.responsebean.CancelOrderResponseBean;
import com.mmlc.utils.ToastUtils;

/**
 * Created by Administrator on 2016/8/16 0016.
 * 申请退款
 */
public class DeleteOrderRequest {

    private Dialog dialog;
    public OrderListUtils orderListUtils;
    OrderButtonBean buttonBean;

    public DeleteOrderRequest(OrderListUtils orderListUtils){
        this.orderListUtils = orderListUtils;
        this.buttonBean = orderListUtils.buttonBean;
    }

    public void deleteOrder(){
        View view = showEdit(R.layout.dialog_delete_order);
        final Button bt_ok = (Button) view.findViewById(R.id.bt_ok);
        final Button bt_cancel = (Button) view.findViewById(R.id.bt_cancel);
        bt_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(dialog.isShowing()){
                    dialog.dismiss();
                }
                deleteOrderRequest(buttonBean.orderId);
            }
        });
        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
    }

    public View showEdit(int layoutId) {
        View view = null;
        if (dialog == null) {
            view = View.inflate(buttonBean.activity, layoutId, null);
            dialog = new Dialog(buttonBean.activity,R.style.centerDialog);
            dialog.setContentView(view);
        }
        dialog.show();
        return view;
    }
    private void deleteOrderRequest(String orderId) {
        DeleteOrderRequestFilter filter = new DeleteOrderRequestFilter(buttonBean.activity);
        filter.requestBean.order_id =orderId;
        filter.offerErrorParams(buttonBean.rootView);
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<CancelOrderResponseBean>() {

            @Override
            public void onSuccess(CancelOrderResponseBean result) {
                ToastUtils.showToast("订单删除成功");

                if(orderListUtils.u != null){
                    int tmpType = -1;
                    if(buttonBean.from == OrderButtonBean.ORDER_DETAIL){//订单详情页
                        tmpType = OrderListUtils.UpdateUI.FINISH;
                    }else{
                        tmpType =  OrderListUtils.UpdateUI.REMOVE_ITEM;
                    }
                    orderListUtils.u.updateUI(buttonBean.itemPositon,tmpType);
                }
            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {
                ToastUtils.showToast(serverError.mess);
            }
        });
    }
}
